<template>
  <div class="person">
    <h2>姓名：{{ person.name }}</h2>
    <h2>年龄：{{ person.age }}</h2>

    <h2>姓名22：{{ name }}</h2>
    <h2>年龄22：{{ age }} {{ nl }}</h2>

    <ul>
      <li v-for="(hobby, index) in person.hobbies" :key="index">{{ hobby }}</li>
    </ul>
    <button @click="person.age++">Age Up</button>
    <button @click="person.hobbies.push('swimming')">Add Hobby</button>
    <button @click="changeName">改变姓名</button>
    <button @click="changeAge">改变年龄</button>
  </div>
</template>

<script lang="ts" setup name="Person">
import { reactive, toRefs, toRef } from 'vue';

let person = reactive({
  name: 'John Doe',
  age: 30,
  hobbies: ['reading', 'traveling', 'hiking']
});

// let {name, age} = person;
let {name, age} = toRefs(person);
let nl = toRef(person, 'age')
console.log(nl, nl.value)

let x = toRefs(person);
console.log(x)

function changeName() {
  name.value += '~'
  console.log(name.value, person.name)
}
function changeAge() {
  age.value += 1
}

</script>

<style scoped>
.person {
  background-color: skyblue;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  padding: 20px
}

button {
  margin: 0 5px;
}
li {
  font-size: 20px;
}
</style>